<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{common/base :: layout(~{::title}, ~{::content}, ~{::style}, ~{::script})}">
<head>
    <title>订单详情 - 后台管理系统</title>
    <style th:fragment="style">
        .status-badge {
            display: inline-block;
            padding: 0.35em 0.65em;
            font-size: 0.75em;
            font-weight: 700;
            line-height: 1;
            color: #fff;
            text-align: center;
            white-space: nowrap;
            vertical-align: baseline;
            border-radius: 0.25rem;
        }
    </style>
</head>
<body>
    <div th:fragment="content">
        <div class="container-fluid py-4">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <h5 class="mb-0">订单详情</h5>
                            <a th:href="@{/admin/orders}" class="btn btn-secondary btn-sm">
                                <i class="bi bi-arrow-left"></i> 返回列表
                            </a>
                        </div>
                        <div class="card-body">
                            <!-- 订单基本信息 -->
                            <div class="row mb-4">
                                <div class="col-md-6">
                                    <h6 class="mb-3">订单信息</h6>
                                    <table class="table table-sm">
                                        <tr>
                                            <th style="width: 150px;">订单编号：</th>
                                            <td th:text="${order.orderNo}"></td>
                                        </tr>
                                        <tr>
                                            <th>订单状态：</th>
                                            <td>
                                                <span th:class="${'badge ' + 
                                                    (order.status == 10 ? 'bg-warning' : 
                                                    order.status == 20 ? 'bg-info' : 
                                                    order.status == 30 ? 'bg-primary' : 
                                                    order.status == 40 ? 'bg-success' : 
                                                    'bg-danger')}"
                                                    th:text="${order.status == 10 ? '待付款' : 
                                                             order.status == 20 ? '已付款' : 
                                                             order.status == 30 ? '已发货' : 
                                                             order.status == 40 ? '已完成' : 
                                                             '已取消'}">
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>创建时间：</th>
                                            <td th:text="${#dates.format(order.createTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
                                        </tr>
                                        <tr th:if="${order.paymentTime != null}">
                                            <th>支付时间：</th>
                                            <td th:text="${#dates.format(order.paymentTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
                                        </tr>
                                        <tr th:if="${order.sendTime != null}">
                                            <th>发货时间：</th>
                                            <td th:text="${#dates.format(order.sendTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
                                        </tr>
                                        <tr th:if="${order.endTime != null}">
                                            <th>完成时间：</th>
                                            <td th:text="${#dates.format(order.endTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
                                        </tr>
                                        <tr th:if="${order.closeTime != null}">
                                            <th>关闭时间：</th>
                                            <td th:text="${#dates.format(order.closeTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="col-md-6">
                                    <h6 class="mb-3">收货信息</h6>
                                    <table class="table table-sm">
                                        <tr>
                                            <th style="width: 150px;">收货人：</th>
                                            <td th:text="${order.shipping.receiver}"></td>
                                        </tr>
                                        <tr>
                                            <th>联系电话：</th>
                                            <td th:text="${order.shipping.phone}"></td>
                                        </tr>
                                        <tr>
                                            <th>收货地址：</th>
                                            <td th:text="${order.shipping.province + order.shipping.city + order.shipping.district + order.shipping.detail}"></td>
                                        </tr>
                                    </table>
                                </div>
                            </div>

                            <!-- 商品信息 -->
                            <div class="row mb-4">
                                <div class="col-12">
                                    <h6 class="mb-3">商品信息</h6>
                                    <div class="table-responsive">
                                        <table class="table table-bordered">
                                            <thead>
                                                <tr>
                                                    <th>商品图片</th>
                                                    <th>商品名称</th>
                                                    <th>单价</th>
                                                    <th>数量</th>
                                                    <th>小计</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr th:each="item : ${order.orderItems}">
                                                    <td style="width: 100px;">
                                                        <img th:src="${item.productImage}" class="img-thumbnail" style="max-width: 80px;">
                                                    </td>
                                                    <td th:text="${item.productName}"></td>
                                                    <td th:text="${#numbers.formatCurrency(item.currentPrice)}"></td>
                                                    <td th:text="${item.quantity}"></td>
                                                    <td th:text="${#numbers.formatCurrency(item.totalPrice)}"></td>
                                                </tr>
                                            </tbody>
                                            <tfoot>
                                                <tr>
                                                    <td colspan="4" class="text-end">运费：</td>
                                                    <td th:text="${#numbers.formatCurrency(order.postage)}"></td>
                                                </tr>
                                                <tr>
                                                    <td colspan="4" class="text-end"><strong>实付金额：</strong></td>
                                                    <td><strong th:text="${#numbers.formatCurrency(order.payment)}"></strong></td>
                                                </tr>
                                            </tfoot>
                                        </table>
                                    </div>
                                </div>
                            </div>

                            <!-- 操作按钮 -->
                            <div class="row">
                                <div class="col-12 text-end">
                                    <button th:if="${order.status == 20}" 
                                            class="btn btn-primary" 
                                            onclick="sendOrder()">
                                        发货
                                    </button>
                                    <button th:if="${order.status == 10}" 
                                            class="btn btn-danger" 
                                            onclick="cancelOrder()">
                                        取消订单
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 发货确认模态框 -->
        <div class="modal fade" id="sendOrderModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">确认发货</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <p>确定要发货吗？</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" onclick="confirmSendOrder()">确定</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 取消订单确认模态框 -->
        <div class="modal fade" id="cancelOrderModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">确认取消订单</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <p>确定要取消该订单吗？</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-danger" onclick="confirmCancelOrder()">确定</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script th:fragment="script">
        const orderNo = [[${order.orderNo}]];
        let sendOrderModal;
        let cancelOrderModal;

        document.addEventListener('DOMContentLoaded', function() {
            sendOrderModal = new bootstrap.Modal(document.getElementById('sendOrderModal'));
            cancelOrderModal = new bootstrap.Modal(document.getElementById('cancelOrderModal'));
        });

        function sendOrder() {
            sendOrderModal.show();
        }

        function confirmSendOrder() {
            fetch(`/admin/orders/${orderNo}/send`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    showToast('success', '发货成功');
                    setTimeout(() => {
                        window.location.reload();
                    }, 1500);
                } else {
                    showToast('error', data.msg || '发货失败');
                }
            })
            .catch(error => {
                showToast('error', '操作失败，请重试');
            })
            .finally(() => {
                sendOrderModal.hide();
            });
        }

        function cancelOrder() {
            cancelOrderModal.show();
        }

        function confirmCancelOrder() {
            fetch(`/admin/orders/${orderNo}/cancel`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    showToast('success', '订单已取消');
                    setTimeout(() => {
                        window.location.reload();
                    }, 1500);
                } else {
                    showToast('error', data.msg || '取消失败');
                }
            })
            .catch(error => {
                showToast('error', '操作失败，请重试');
            })
            .finally(() => {
                cancelOrderModal.hide();
            });
        }

        function showToast(type, message) {
            const toast = document.createElement('div');
            toast.className = `toast align-items-center text-white bg-${type === 'success' ? 'success' : 'danger'} border-0 position-fixed top-0 end-0 m-3`;
            toast.setAttribute('role', 'alert');
            toast.setAttribute('aria-live', 'assertive');
            toast.setAttribute('aria-atomic', 'true');
            
            toast.innerHTML = `
                <div class="d-flex">
                    <div class="toast-body">
                        ${message}
                    </div>
                    <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
                </div>
            `;
            
            document.body.appendChild(toast);
            const bsToast = new bootstrap.Toast(toast);
            bsToast.show();
            
            toast.addEventListener('hidden.bs.toast', () => {
                document.body.removeChild(toast);
            });
        }
    </script>
</body>
</html> 